package com.acmen.easyapi.service;

import com.acmen.easyapi.dto.TestDataQry;
import com.acmen.easyapi.dto.UnitTestCmd;
import com.acmen.easyapi.executor.UnitTestDelExe;
import com.acmen.easyapi.executor.UnitTestExe;
import com.acmen.easyapi.executor.UnitTestSaveExe;
import com.acmen.easyapi.model.Response;
import com.acmen.easyapi.dto.UnitTestAdd;
import com.acmen.easyapi.repository.UnitTestRepository;
import com.acmen.easyapi.rpc.RunLogRemoteService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * @author acmen
 */
@Service
public class UnitTestService {
    @Autowired
    private UnitTestExe unitTestExe;
    @Autowired
    private UnitTestRepository unitTestRepository;
    @Autowired
    private UnitTestDelExe unitTestDelExe;
    @Resource
    private RunLogRemoteService runLogRemoteService;
    @Resource
    private UnitTestSaveExe unitTestSaveExe;
    /**
     * 获取接口调用结果
     *
     * @param unitTestCmd
     * @return
     */
    public Response test(UnitTestCmd unitTestCmd, HttpServletResponse response) {
        return unitTestExe.test(unitTestCmd,response);
    }

    public Response getTestData(TestDataQry testDataQry) {
        return unitTestExe.getTestData(testDataQry);
    }

    public Response<List<UnitTestAdd>> selectList(String apiId) {
        return Response.of(unitTestRepository.selectList(apiId));
    }

    public Response save(UnitTestAdd unitTest) {
        unitTest.setId(UUID.randomUUID().toString().replace("-",""));
        return unitTestSaveExe.save(unitTest);
    }

    public Response joinPressureTest(String id,boolean join) {
        return unitTestSaveExe.joinPressureTest(id,join);
    }


    /**
     * 删除单元测试
     *
     * @param id
     * @return
     */
    public Response delete(String id) {
        return unitTestDelExe.delete(id);
    }



    public Response<JSONObject> getTestLog(String traceId,String jwt){
        return runLogRemoteService.selectByTraceId(traceId,jwt);
    }

}
